package com.sgb.kjwl.view.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.apeng.permissions.EsayPermissions;
import com.apeng.permissions.OnPermission;
import com.mob.MobSDK;
import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AskLeaseFactoryEntity;
import com.sgb.kjwl.model.entity.AskLeaseFarmEntity;
import com.sgb.kjwl.model.entity.AskLeaseLandEntity;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.model.entity.self.LeaseFactoryEntity;
import com.sgb.kjwl.model.entity.self.LeaseFarmEntity;
import com.sgb.kjwl.model.entity.self.LeaseHousesEntity;
import com.sgb.kjwl.model.entity.self.LeaseLandEntity;
import com.sgb.kjwl.model.entity.user.AddCarEntity3;
import com.sgb.kjwl.model.entity.user.AddDeviceEntity;
import com.sgb.kjwl.model.entity.user.AddMachineOutEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ResolutionUtil;
import com.swgk.core.util.qrcode.QRCodeUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.dingding.friends.Dingding;
import cn.sharesdk.dingding.utils.DingdingClientNotExistException;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qq.QQClientNotExistException;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 租赁分享-分享底部弹窗实现
 */
public class ShareLeaseDialog {
    //    private TextView weixins;
//    private TextView cancels;
    private String url;
    private String title;
    private String text;
    private String imgUrl;

    private TextView tvTitle;
    private TextView oneTitle;
    private TextView tv_oneName;
    private TextView twoTitle;
    private TextView tv_twoName;
    private TextView threeTitle;
    private TextView tv_threeName;
    private ImageView iv_qr_code;

    private Activity activity;
    //    private String base_url = "https://h5-zulin.gcw.net/";
    private Dialog dialog;
    private View rootView;

//    private Platform platform;

    public ShareLeaseDialog(Activity Context) {
        this.activity = Context;
        MobSDK.submitPolicyGrantResult(true, null);

        dialog = new Dialog(Context, R.style.RoundCornerDialog);
        //2、设置布局
        rootView = View.inflate(Context, R.layout.share_lease_dialog, null);
//        weixins = view.findViewById(R.id.tv_wechat);
//        cancels = view.findViewById(R.id.cancel);
        tvTitle = rootView.findViewById(R.id.title);
        oneTitle = rootView.findViewById(R.id.one_title);
        tv_oneName = rootView.findViewById(R.id.one_name);
        twoTitle = rootView.findViewById(R.id.two_title);
        tv_twoName = rootView.findViewById(R.id.two_name);
        threeTitle = rootView.findViewById(R.id.three_title);
        tv_threeName = rootView.findViewById(R.id.three_name);
        iv_qr_code = rootView.findViewById(R.id.iv_qr_code);

        dialog.setContentView(rootView);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();


//        //微信
//        weixins.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//                if (TextUtils.isEmpty(title) && TextUtils.isEmpty(text)) {
//                    MToast.showToast(activity, "分享失败");
//                    return;
//                }
//                ShareSDKUtilsEntity bean = new ShareSDKUtilsEntity();
//                bean.setTitle(title);
//                bean.setText(text);
//                if (TextUtils.isEmpty(imgUrl)) {
//                    bean.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.ic_wx_ask_lease));
//                } else {
//                    bean.setImageUrl(imgUrl);
//                }
//                bean.setUrl(url);
//                bean.setShareType(Wechat.NAME);
//                bean.setToastText("微信");
//                bean.setToastShow(true);
//                ShareSDKUtils.ShareShow(Context, bean);
//            }
//        });
//
//
//        // 取消
//        cancels.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
        initView();
    }

    private void initView() {
        rootView.findViewById(R.id.tv_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(Wechat.NAME));
            }
        });
        rootView.findViewById(R.id.tv_wechat_moments).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(WechatMoments.NAME));
            }
        });
        rootView.findViewById(R.id.tv_dingding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(Dingding.NAME));
            }
        });
        rootView.findViewById(R.id.tv_qq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(QQ.NAME));
            }
        });
        rootView.findViewById(R.id.tv_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", url);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                MToast.showToast(activity, "复制链接成功！");
                dialog.dismiss();
            }
        });
        rootView.findViewById(R.id.tv_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean hasPermission = EsayPermissions.isHasPermission(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (hasPermission) {
                    saveBitmapFromView(rootView.findViewById(R.id.pic_layout));
                } else {
                    EsayPermissions.with(activity).permission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE).request(new OnPermission() {
                        @Override
                        public void hasPermission(List<String> granted, boolean isAll) {
                            saveBitmapFromView(rootView.findViewById(R.id.pic_layout));
                        }

                        @Override
                        public void noPermission(List<String> denied, boolean quick) {
                            MToast.showToast(activity, "存储权限未开启 !");
                        }
                    });
                }
            }
        });

        rootView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        rootView.findViewById(R.id.rel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }


    public synchronized void saveBitmapFromView(View view) {
        int w = view.getWidth();
        int h = view.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);
        view.draw(c);
        saveImageToGallery(bmp, title + ".jpeg");
    }

    public void saveImageToGallery(Bitmap bitmap, String bitName) {
        String storePath =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/SGB/";
//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Pictures";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        File file = new File(storePath, bitName);
        if (file.exists()) {
            file.delete();
        }

        try {
            FileOutputStream out = new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
                // 插入图库，不生成缩略图
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                //通知相册有新图片，提醒系统检索导出的图片
                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri contentUri = Uri.fromFile(file);
                mediaScanIntent.setData(contentUri);
                activity.sendBroadcast(mediaScanIntent);
                MToast.showToast(activity, "保存图片成功！");
                dialog.dismiss();
            } else {
                MToast.showToast(activity, "保存图片失败！");
            }
        } catch (Exception e) {
            MToast.showToast(activity, "系统版本过低，保存图片失败！");
            e.printStackTrace();
        }
    }

    public void init(String leaseType, String categoryType, String id) {
        if (TextUtils.isEmpty(categoryType)) return;
        if ("lease".equals(leaseType)) {//出租
            if ("1".equals(categoryType)) {
                getMachineOutDetailModel(id);
//                url = base_url + "mechanicsDetail?id=" + id;
            } else if ("2".equals(categoryType)) {
                getDetailModel(id);
//                url = base_url + "equipmentDetails?id=" + id;
            } else if ("3".equals(categoryType)) {
                getCarDetail(id);
//                url = base_url + "vehicleDetails?id=" + id;
            } else if ("4".equals(categoryType)) {
                getEditDetailHouse(id);
//                url = base_url + "propertyDetails?id=" + id;
            } else if ("5".equals(categoryType)) {
                getEditDetailLand(id);
//                url = base_url + "landDetails?id=" + id;
            } else if ("6".equals(categoryType)) {
                getEditDetailFactory(id);
//                url = base_url + "factoryDetails?id=" + id;
            } else if ("7".equals(categoryType)) {
                getEditDetailFarm(id);
//                url = base_url + "farmDetail?id=" + id;
            }
        } else {//求租
            if ("1".equals(categoryType)) {
                getDeviceDetailAskLease(id, String.valueOf(Integer.parseInt(categoryType) - 1));
//                url = base_url + "mechanicsRentDetails?id=" + id;
            } else if ("2".equals(categoryType)) {
                getDeviceDetailAskLease(id, String.valueOf(Integer.parseInt(categoryType) - 1));
//                url = base_url + "equipmentRentDetails?id=" + id;
            } else if ("3".equals(categoryType)) {
                getDeviceDetailAskLease(id, String.valueOf(Integer.parseInt(categoryType) - 1));
//                url = base_url + "vehicleRentDetails?id=" + id;
            } else if ("4".equals(categoryType)) {
                getDeviceDetailAskLease(id, String.valueOf(Integer.parseInt(categoryType) - 1));
//                url = base_url + "houseRentDetails?id=" + id;
            } else if ("5".equals(categoryType)) {
                getDetailAskLeaseLand(id);
//                url = base_url + "landRentDetails?id=" + id;
            } else if ("6".equals(categoryType)) {
                getDetailAskLeaseFactory(id);
//                url = base_url + "factoryRentDetails?id=" + id;
            } else if ("7".equals(categoryType)) {
                getDetailAskLeaseFarm(id);
//                url = base_url + "farmRentDetails?id=" + id;
            }
        }
    }


    private void ShareSDKContent(Platform platform) {

//        Bitmap Bmp = BitmapFactory.decodeResource(activity, imgUrl);
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(title);
        sp.setText(text);
//        sp.setImageData(Bmp);

        if (TextUtils.isEmpty(imgUrl)) {
            sp.setImageData(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.ic_wx_ask_lease));
        } else {
            sp.setImageUrl(imgUrl);
        }
        sp.setUrl(url);
        sp.setTitleUrl(url);
        sp.setShareType(Platform.SHARE_WEBPAGE);

        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                MToast.showToast(activity, "分享成功！");
                dialog.dismiss();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                dialog.dismiss();
                String msg = "软件未安装";
                if (throwable instanceof WechatClientNotExistException) {
                    msg = "微信未安装！";
                }
                if (throwable instanceof DingdingClientNotExistException) {
                    msg = "钉钉未安装！";
                }
                if (throwable instanceof QQClientNotExistException || "QQClientNotExistException".equals(throwable.getMessage())) {
                    msg = "QQ未安装！";
                }
                String finalMsg = msg;
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MToast.showToast(activity, finalMsg);
                    }
                });
            }

            @Override
            public void onCancel(Platform platform, int i) {
                MToast.showToast(activity, "分享取消！");
                dialog.dismiss();
            }
        });
        platform.share(sp);
    }

    //获取机械出租详情
    public void getMachineOutDetailModel(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPublishMachineOutDetail(id).enqueue(
                new Callback<BaseEntity<AddMachineOutEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<AddMachineOutEntity>> call, Response<BaseEntity<AddMachineOutEntity>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) return;
                        AddMachineOutEntity entity = response.body().getData();
                        if (entity != null) {
                            url = entity.getWeixinFenXiangUrl() + "mechanicsDetail?id=" + id;
                            title = entity.getTitle();
                            text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n";
                            if (entity.getLocation() != null) {
                                text += entity.getLocation().getCityName() + "·" + entity.getLocation().getAreaName();
                            }
                            if (entity.getPictures() != null && entity.getPictures().size() > 0) {
                                imgUrl = entity.getPictures().get(0);
                            }
                            tvTitle.setText(title);
                            oneTitle.setText("机械类型：");
                            tv_oneName.setText(TextUtils.isEmpty(entity.getSecondName()) ? entity.getFirstName() : entity.getSecondName());
                            twoTitle.setText("租赁价格：");
                            if ("1".equals(entity.getNegotiable())) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getPriceUnitName());
                            }
                            threeTitle.setText("出租方：");
                            if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                                tv_threeName.setText(entity.getCreateCompanyName());
                            } else {
                                tv_threeName.setText(entity.getCreateUserName());
                            }
                            iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<AddMachineOutEntity>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(activity, "请求服务器失败");
                    }
                }
        );
    }


    //获取设备出租详情
    public void getDetailModel(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDeviceAPI().getDeviceDetailShare(id).enqueue(
                new Callback<BaseEntity<AddDeviceEntity>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<AddDeviceEntity>> call, Response<BaseEntity<AddDeviceEntity>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) return;
                        if (response.body().getData() != null && APIManager.RESPONSE_OK.equals(response.body().getState())) {
                            AddDeviceEntity entity = response.body().getData();
                            url = entity.getWeixinFenXiangUrl() + "equipmentDetails?id=" + id;
                            title = entity.getTitle();
                            text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n";
                            if (entity.getLocation() != null) {
                                text += entity.getLocation().getCityName() + "·" + entity.getLocation().getAreaName();
                            }
                            if (entity.getPictures() != null && entity.getPictures().size() > 0) {
                                imgUrl = entity.getPictures().get(0);
                            }
                            tvTitle.setText(title);
                            oneTitle.setText("设备类型：");
                            tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                            twoTitle.setText("租赁价格：");
                            if ("1".equals(entity.getNegotiable())) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getPriceUnitName());
                            }
                            threeTitle.setText("出租方：");
                            if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                                tv_threeName.setText(entity.getCreateCompanyName());
                            } else {
                                tv_threeName.setText(entity.getCreateUserName());
                            }
                            iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<AddDeviceEntity>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(activity, "请求服务器失败");
                    }
                }
        );
    }

    //车辆出租详情
    public void getCarDetail(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getCarPublishDetail(id)
                .enqueue(new Callback<BaseEntity<AddCarEntity3>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<AddCarEntity3>> call, Response<BaseEntity<AddCarEntity3>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) return;
                        if (response.body().getData() != null && APIManager.RESPONSE_OK.equals(response.body().getState())) {
                            try {
                                AddCarEntity3 entity = response.body().getData();
                                url = entity.getWeixinFenXiangUrl() + "vehicleDetails?id=" + id;
                                title = entity.getTitle();
                                text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n"
                                        + entity.getAddCarStoreEntity3().getPickupAreaEntity3().getCityName()
                                        + "·" + entity.getAddCarStoreEntity3().getPickupAreaEntity3().getDistrictName();
                                if (entity.getPictures() != null && entity.getPictures().size() > 0) {
                                    imgUrl = entity.getPictures().get(0);
                                }
                                tvTitle.setText(title);
                                oneTitle.setText("车辆车型：");
                                tv_oneName.setText(entity.getCarTypeLabel());
                                twoTitle.setText("租赁价格：");
                                //设置租金
                                try {
                                    String price = entity.getAddCarLeaseEntity3().getLeaseDayMoney();
                                    if (Double.parseDouble(price) < 0) {
                                        tv_twoName.setText("面议");
                                    } else {
                                        tv_twoName.setText(price + "元/天");
                                    }
                                } catch (Exception ex) {
                                    tv_twoName.setText("面议");
                                }
                                threeTitle.setText("出租方：");
                                if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                                    tv_threeName.setText(entity.getCreateCompanyName());
                                } else {
                                    tv_threeName.setText(entity.getCreateUserName());
                                }
                                iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                            } catch (Exception ex) {
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<AddCarEntity3>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(activity, "请求服务器失败");
                    }
                });
    }

    //房产出租编辑详情
    public void getEditDetailHouse(String id) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getDetailHouse(id).enqueue(new Callback<BaseEntity<LeaseHousesEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LeaseHousesEntity>> call, Response<BaseEntity<LeaseHousesEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        LeaseHousesEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "propertyDetails?id=" + id;
                        title = entity.getTitle();
                        text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                        if (entity.getImgs() != null && entity.getImgs().size() > 0) {
                            imgUrl = entity.getImgs().get(0);
                        }
                        tvTitle.setText(title);
                        oneTitle.setText("房产类型：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("租赁价格：");
                        //设置租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getRentalSelectPriceName());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("出租方：");
                        if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                            tv_threeName.setText(entity.getCreateCompanyName());
                        } else {
                            tv_threeName.setText(entity.getPublisherName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                    } else {
                        MToast.showToast(activity, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LeaseHousesEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "请求服务器失败");
            }
        });
    }

    //土地出租编辑详情
    public void getEditDetailLand(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getDetailLand(id).enqueue(new Callback<BaseEntity<LeaseLandEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LeaseLandEntity>> call, Response<BaseEntity<LeaseLandEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (null != response.body().getData()) {
                        LeaseLandEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "landDetails?id=" + id;
                        title = entity.getTitle();
                        text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                        if (entity.getImgs() != null && entity.getImgs().size() > 0) {
                            imgUrl = entity.getImgs().get(0);
                        }
                        tvTitle.setText(title);
                        oneTitle.setText("土地类型：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("租赁价格：");
                        //设置租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getRentalSelectPriceName());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("出租方：");
                        if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                            tv_threeName.setText(entity.getCreateCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                    } else {
                        MToast.showToast(activity, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LeaseLandEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "请求服务器失败");
            }
        });
    }

    //工厂详情
    public void getEditDetailFactory(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getDetailFactory(id).enqueue(new Callback<BaseEntity<LeaseFactoryEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LeaseFactoryEntity>> call, Response<BaseEntity<LeaseFactoryEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        LeaseFactoryEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "factoryDetails?id=" + id;
                        title = entity.getTitle();
                        text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                        if (entity.getImgPathList() != null && entity.getImgPathList().size() > 0) {
                            imgUrl = entity.getImgPathList().get(0);
                        }
                        tvTitle.setText(title);
                        oneTitle.setText("工厂分类：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("租赁价格：");
                        //设置租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getPriceUnitName());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("出租方：");
                        if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                            tv_threeName.setText(entity.getCreateCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                    } else {
                        MToast.showToast(activity, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LeaseFactoryEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "请求服务器失败");
            }
        });
    }

    //农机出租编辑详情
    public void getEditDetailFarm(String id) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().detailFarm(id).enqueue(new Callback<BaseEntity<LeaseFarmEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LeaseFarmEntity>> call, Response<BaseEntity<LeaseFarmEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        LeaseFarmEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "farmDetail?id=" + id;
                        title = entity.getTitle();
                        text = "信息发布时间：" + DateUtils.stampToDay(entity.getUpdateTime()) + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                        if (entity.getImgPathList() != null && entity.getImgPathList().size() > 0) {
                            imgUrl = entity.getImgPathList().get(0);
                        }
                        tvTitle.setText(title);
                        oneTitle.setText("农机类型：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("租赁价格：");
                        //设置租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                tv_twoName.setText(entity.getRentalPrice() + entity.getPriceUnitName());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("出租方：");
                        if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                            tv_threeName.setText(entity.getCreateCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                    } else {
                        MToast.showToast(activity, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LeaseFarmEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "请求服务器失败");
            }
        });
    }

    //机械。设备，车辆，房产 求租详情
    public void getDeviceDetailAskLease(String id, String type) {
        DialogHelper.showProgressDialog(activity, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().getDeviceDetail(id, type).enqueue(new Callback<BaseEntity<PublishLeaseDataEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PublishLeaseDataEntity>> call, Response<BaseEntity<PublishLeaseDataEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    try {
                        PublishLeaseDataEntity entity = response.body().getData();
                        title = entity.getRentalTitle();
                        tv_oneName.setText(TextUtils.isEmpty(entity.getCategoryChildName()) ? entity.getCategoryParentName() : entity.getCategoryChildName());
                        if ("0".equals(type)) {
                            url = entity.getWeixinFenXiangUrl() + "mechanicsRentDetails?id=" + id;
                            oneTitle.setText("机械类型：");
                        } else if ("1".equals(type)) {
                            url = entity.getWeixinFenXiangUrl() + "equipmentRentDetails?id=" + id;
                            oneTitle.setText("设备类型：");
                        } else if ("2".equals(type)) {
                            url = entity.getWeixinFenXiangUrl() + "vehicleRentDetails?id=" + id;
                            oneTitle.setText("车辆车型：");
                            tv_oneName.setText(entity.getRentalModelName());
                        } else if ("3".equals(type)) {
                            url = entity.getWeixinFenXiangUrl() + "houseRentDetails?id=" + id;
                            oneTitle.setText("房产类型：");
                        }
                        tvTitle.setText(title);

                        twoTitle.setText("期望租金：");
                        //设置期望租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                if (TextUtils.isEmpty(entity.getRentalDurationUnit())) {
                                    entity.setRentalDurationUnit("天");
                                }
                                tv_twoName.setText(entity.getRentalPrice() + "元/" + entity.getRentalDurationUnit());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("需求方：");
                        if (!TextUtils.isEmpty(entity.getCreateCompanyName())) {
                            tv_threeName.setText(entity.getCreateCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        //分享出去的内容
                        text = oneTitle.getText().toString() + tv_oneName.getText() + "\n" + twoTitle.getText() + tv_twoName.getText() + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                    } catch (Exception ex) {
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PublishLeaseDataEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取土地求租详情
    public void getDetailAskLeaseLand(String id) {
        DialogHelper.showProgressDialog(activity, "加载中", 0, false);
        APIManager.getInstance().getMachineAPI().getDetailLand(id).enqueue(new Callback<BaseEntity<AskLeaseLandEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseLandEntity>> call, Response<BaseEntity<AskLeaseLandEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        AskLeaseLandEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "landRentDetails?id=" + id;
                        title = entity.getTitle();

                        tvTitle.setText(title);
                        oneTitle.setText("土地类型：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("期望租金：");
                        //设置期望租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                if (TextUtils.isEmpty(entity.getRentalDurationUnit())) {
                                    entity.setRentalDurationUnit("天");
                                }
                                tv_twoName.setText(entity.getRentalPrice() + "元/" + entity.getRentalDurationUnit());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("需求方：");
                        if (!TextUtils.isEmpty(entity.getCompanyName())) {
                            tv_threeName.setText(entity.getCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        //分享出去的内容
                        text = oneTitle.getText().toString() + tv_oneName.getText() + "\n" + twoTitle.getText() + tv_twoName.getText() + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseLandEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取工厂求租详情
    public void getDetailAskLeaseFactory(String id) {
        DialogHelper.showProgressDialog(activity, "加载中", 0, false);
        APIManager.getInstance().getMachineAPI().getDetailFactory(id).enqueue(new Callback<BaseEntity<AskLeaseFactoryEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseFactoryEntity>> call, Response<BaseEntity<AskLeaseFactoryEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        AskLeaseFactoryEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "factoryRentDetails?id=" + id;
                        title = entity.getTitle();

                        tvTitle.setText(title);
                        oneTitle.setText("工厂分类：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("期望租金：");
                        //设置期望租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                if (TextUtils.isEmpty(entity.getRentalDurationUnit())) {
                                    entity.setRentalDurationUnit("天");
                                }
                                tv_twoName.setText(entity.getRentalPrice() + "元/" + entity.getRentalDurationUnit());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }
                        threeTitle.setText("需求方：");
                        if (!TextUtils.isEmpty(entity.getCompanyName())) {
                            tv_threeName.setText(entity.getCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        //分享出去的内容
                        text = oneTitle.getText().toString() + tv_oneName.getText() + "\n" + twoTitle.getText() + tv_twoName.getText() + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseFactoryEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取农机求租详情
    public void getDetailAskLeaseFarm(String id) {
        DialogHelper.showProgressDialog(activity, "加载中", 0, false);
        APIManager.getInstance().getMachineAPI().getDetailFarm(id).enqueue(new Callback<BaseEntity<AskLeaseFarmEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseFarmEntity>> call, Response<BaseEntity<AskLeaseFarmEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        AskLeaseFarmEntity entity = response.body().getData();
                        url = entity.getWeixinFenXiangUrl() + "farmRentDetails?id=" + id;
                        title = entity.getTitle();
                        tvTitle.setText(title);
                        oneTitle.setText("农机类型：");
                        tv_oneName.setText(TextUtils.isEmpty(entity.getSecondCategoryName()) ? entity.getFirstCategoryName() : entity.getSecondCategoryName());
                        twoTitle.setText("期望租金：");
                        //设置期望租金
                        try {
                            if (Double.parseDouble(entity.getRentalPrice()) < 0) {
                                tv_twoName.setText("面议");
                            } else {
                                if (TextUtils.isEmpty(entity.getRentalDurationUnit())) {
                                    entity.setRentalDurationUnit("天");
                                }
                                tv_twoName.setText(entity.getRentalPrice() + "元/" + entity.getRentalDurationUnit());
                            }
                        } catch (Exception ex) {
                            tv_twoName.setText("面议");
                        }

                        threeTitle.setText("需求方：");
                        if (!TextUtils.isEmpty(entity.getCompanyName())) {
                            tv_threeName.setText(entity.getCompanyName());
                        } else {
                            tv_threeName.setText(entity.getCreateUserName());
                        }
                        iv_qr_code.setImageBitmap(QRCodeUtils.createQRCode(url, ResolutionUtil.dp2px(activity, 61), ResolutionUtil.dp2px(activity, 61), true));
                        //分享出去的内容
                        text = oneTitle.getText().toString() + tv_oneName.getText() + "\n" + twoTitle.getText() + tv_twoName.getText() + "\n"
                                + entity.getCityName() + "·" + entity.getAreaName();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseFarmEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}